import maintenanceOrderList from './components/maintenanceOrderList.vue' //售后
import defaultImg from '@/components/defaultImg/defaultImg'
import buyList from './components/buyCarList.vue'//购车列表
import appointmentList from './components/appointmentList.vue'//预约列表
import findCarList from './components/finCarList.vue'//寻车列表
import timLogin from '@/utils/timLogin.js' // 导入tim 登录
export default {
  data() {
    return {
      pageLoading: false,
      maintenanceStatuss: '', //维保单加载更多
      isdefault: false, //首付默认图
      defaultYy: false, //预约到店
      findDefault: false, //询价
      cancelText: '取消预约',
      tabTop: false,
      enable: true, //开启吸顶
      // 自定义下拉刷新
      currentTab: 0, //tab
      collectionId: '',
      isCollection: false, //确认提车模态
      id: '', //取消id
      show: false, //预约模态框
      current3: 0, //预约tabs
      findCurrent: 0, //寻车tabs
      color: [],
      appointmentList: [],
      //预约到店tabs
      yYlist: [{
        name: '全部'
      },
      {
        name: '待到店'
      },
      {
        name: '已到店',
      },
      ],
      listTab: [{
        name: '购车订单'
      },
      {
        name: '售后维保订单'
      }

      ],
      //swiper列表
      listTitle: [{
        name: '1成首付/以租代售'
      }, {
        name: '我的询价'
      },
      {
        name: '预约到店'
      }

      ],
      //寻车订单tabs
      findOrder: [{
        name: '全部'
      },
      {
        name: '待报价'
      },
      {
        name: '待确认',
      },
      {
        name: '完成',
      },
      ],
      // 维保售后订单tab
      maintenNavlist: [{
        name: '全部'
      },
      {
        name: '待付款'
      },
      {
        name: '待备货'
      },
      {
        name: '待服务'
      },
      {
        name: '服务中'
      },
      {
        name: '已完成'
      }
      ],
      maintenance: {
        pageIndex: 1,
        pageSize: 10,
        status: -1,
      },
      maintenanceTotal: 0,
      maintenCurNow: 0, //当前选项
      maintenanceList: [], //维保订单列表
      current: 0, //全屏选项当前
      swiperCurrent: 0, //swiper当前
      dx: 0,
      // orderListHeight: '100vh',
      current2: 0, //tabs
      color: [],
      //tabs购车列表
      list: [{
        name: '全部',
        id: 0,
      },
      {
        name: '待审核',
        id: 1,
      },
      {
        name: '未通过',
        id: 10,
      },
      {
        name: '待补资料',
        id: 15,
      },
      {
        name: '待付款',
        id: 30,
      },
      {
        name: '待提车',
        id: 40,
      },
      {
        name: '已完成',
        id: 45,
      }
      ],
      tabsId: 0,
      resetTimes: [], //购车列表数据
      statuss: 'loading', //购车状态

      pageIndex: 1, //购车当前页
      mentPageIndex: 1, //预约当前页
      findList: [], //寻车订单列表数据

      findAllIndex: 1, //寻车全部当前页
      showWb: false,
      showPageCar: false,
      showPageMaintenance: false
    }
  },
  components: {
    maintenanceOrderList,
    defaultImg,
    buyList,
    appointmentList,
    findCarList
  },
  filters: {
    million(value) { //过万处理
      let num
      if (value > 9999) { //大于9999显示x.xx万
        num = ((value / 1000) / 10).toFixed(2) + '万'
      } else if (value <= 9999 && value >= -9999) {
        num = value + '元'
      } else if (value < -9999) { //小于-9999显示-x.xx万
        num = -((Math.abs(value) / 1000) / 10).toFixed(2) + '万'
      }
      return num
    },
    formatStoreType(data) {
      let text = ''
      switch (Number(data)) {
      case 0:
      case 1:
        text = '待审核'
        break
      case 10:
        text = '未通过'
        break
      case 15:
        text = '待补资料'
        break
      case 30:
        text = '待付款'
        break
      case 40:
        text = '待提车'
        break
      case 45:
        text = '已完成'
        break
      default:
        text = ''
        break
      }
      return text
    }
  },
  onLoad(op) {
    if (op.index) {
      this.currentTab = Number(op.index)
      // if(this.currentTab===1){
      //   this.getMyCarFindList()
      // }else if(this.currentTab===2){
      //   this.getCarBookPageList()
      // }
    }
    if (op.swiper) {
      this.swiperCurrent = Number(op.swiper)
      this.current = Number(op.swiper)
    }
  },
  onHide() {
    this.enable = false
  },
  onShow() {
    let token = uni.getStorageSync('token')
    this.enable = true
    if (!token) {
      this.$u.toast('请登录！')
      // #ifdef  MP-WEIXIN
      uni.setStorage({
        key: 'launchFlag',
        data: false,
      })
      uni.redirectTo({
        url: '/pages_logins/login/login'
      })
      // #endif
      // #ifndef  MP-WEIXIN
      // this.$u.debounce(this.login, 1000)
      // #endif
      return false
    } else {
      // this.maintenance.status = -1
      this.maintenance.pageIndex = 1
      this.maintenanceList = []
      this.getOrderMaintenanceList()
      if (this.currentTab === 0) {
        this.pageIndex = 1
        this.resetTimes = []
        this.getMyOrderBuyCarAllList()
      } else if (this.currentTab === 1) {
        this.findList = []
        if (this.findCurrent === 0) {
          this.findAllIndex = 1
          this.getMyCarFindList()
        } else if (this.findCurrent === 1) {
          this.findAllIndex = 1
          this.getMyCarFindNotQuoteList()
        } else if (this.findCurrent === 2) {
          this.findAllIndex = 1
          this.getMyCarFindNotCertainList()
        } else if (this.findCurrent === 3) {
          this.findAllIndex = 1
          this.getMyCarFindFinishedList()
        }
      } else if (this.currentTab === 2) {
        this.mentPageIndex = 1
        this.appointmentList = []
        this.getCarBookPageList()
      }
    }

  },
  onTabItemTap(e) {
    console.log(e.index)
    if(e.index === 3 && !uni.getStorageSync('token')){
      uni.setStorage({
        key: 'launchFlag',
        data: false,
      })
      // uni.hideTabBar()
      uni.redirectTo({
        url: '/pages_logins/login/login',
        success: () => {
          let t = setTimeout(() => {
            uni.hideLoading()
            clearTimeout(t)
          }, 300)
        }
      })
    }
  },
  created() {
    /* let token = uni.getStorageSync('token')
    if(!token){
      this.$u.toast('请登录！')
      this.$u.debounce(this.login, 1000)
      return false
    }else{
      this.maintenance.status = -1
      this.maintenance.pageIndex = 1
      this.maintenanceList = []
      this.getOrderMaintenanceList()
    }
 */
  },
  methods: {
    //店铺详情
    onStore(id) {
      uni.navigateTo({
        url: '/pages_store/newCarStore/index/index?storeid=' + id
      })
    },
    //特权
    tq() {
      uni.navigateTo({
        url: '/pages_order/hittiCar/hittiCar'
      })
    },
    //登录
    login() {
      // uni.showTabBar({
      //   animation: true
      // })
      uni.hideTabBar()
      uni.setStorage({
        key: 'launchFlag',
        data: false,
      })
      uni.navigateTo({
        url: '/pages_logins/login/login'
      })
    },
    //购车加载更多（点击）
    buyCarMore() {
      this.getMyOrderBuyCarAllList()
    },
    //询价更多
    findMore() {
      if (this.findCurrent === 0) {
        this.getMyCarFindList()
      } else if (this.findCurrent === 1) {
        this.getMyCarFindNotQuoteList()
      } else if (this.findCurrent === 2) {
        this.getMyCarFindNotCertainList()
      } else if (this.findCurrent === 3) {
        this.getMyCarFindFinishedList()
      }
    },
    //预约更多
    appointmentMore() {
      this.getCarBookPageList()
    },

    //轮播定位
    swierChange(e) {
      this.swiperCurrent = e.detail.current
    },
    // tab栏切换
    change(index) {
      this.swiperCurrent = index
      console.log(index)
    },

    // 我的询价
    getCont2(getList) {
      this.$store.commit('news/createConversationActive', {
        user: getList.trueName, //店铺名称
        img: getList.headPortrait, //店铺头像
        lastTime: '', //
        userId: getList.id, //店铺id
        conversationID: 'C2C'
      })
      /* this.$store.commit('news/setCardInfo', {
			  imgSrc: getList.brandUrl,
			  name: getList.carName,
			  price: getList.factoryPrice,
			  type: 'resaleCar',
			  goPage: '2'
      }) */
      uni.navigateTo({
        url: '/pages_common/contactSeller/contactSeller?user=' + getList.storeName + '&pageId=' +
					getList.id + '&tab=2'
        // '/pages_common/contactSeller/contactSeller?user=' + types.storeName + '&pageId=' + this.bookid+ '&tab=0'
      })
      uni.navigateTo({
        url: '/pages_common/room/room?user=' + getList.trueName
      })
    },
    contact2(getList) {
      if (this.$store.state.isSDKReady) {
        this.getCont2(getList)
      } else {
        timLogin(() => {
          this.getCont2(getList)
        })
      }

    },

    scrollTo() {
      uni.pageScrollTo({
        duration: 0,
        scrollTop: 0
      })
    },
    current2Change(e) {
      this.scrollTo()
      this.currentTab = e
      if (e === 0) {
        this.pageIndex = 1
        this.resetTimes = []
        this.getMyOrderBuyCarAllList()
      } else if (e === 1) {
        this.getMyCarFindList()
      } else if (e === 2) {
        this.mentPageIndex = 1
        this.appointmentList = []
        this.getCarBookPageList()
      }
    },

    //一成首付上拉加载
    findScrolltolower() {
      if (this.findCurrent === 0) {
        this.getMyCarFindList()
      } else if (this.findCurrent === 1) {
        this.getMyCarFindNotQuoteList()
      } else if (this.findCurrent === 2) {
        this.getMyCarFindNotCertainList()
      } else if (this.findCurrent === 3) {
        this.getMyCarFindFinishedList()
      }
    },

    // 预约加载更多
    appointmentMore() {
      this.getCarBookPageList()

    },
    //购车加载更多
    scrolltolower() {
      this.getMyOrderBuyCarAllList()
    },
    // 加载前、中、后
    moreStatus(status) {
      this.statuss = status
    },
    //购车订单tab切换
    changeTabs(index) {
      this.scrollTo()
      this.pageIndex = 1
      this.resetTimes = [] // 清空列表

      this.tabsId = index
      this.current2 = index
      this.getMyOrderBuyCarAllList()
      // this.$u.throttle(this.getMyOrderBuyCarAllList, 500)
    },
    //购车订单数据
    async getMyOrderBuyCarAllList() {
      this.moreStatus('loading')
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        let data = {
          'pageIndex': this.pageIndex,
          'pageSize': 10,
          'status': this.tabsId - 1
        }
        const res = await this.$api.order.getMyOrderBuyCarAllList(data)
        this.resetTimes = [...this.resetTimes, ...res.data.list]
        uni.stopPullDownRefresh()
        this.pageLoading = true
        uni.hideLoading()
        if (this.resetTimes.length === 0) {
          this.isdefault = true
        } else {
          this.isdefault = false
        }
        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) {

          return false
        }
        this.pageIndex += 1 // 当前页加1
      } catch (e) {
        uni.hideLoading()
      }
    },
    // 售后维保订单列表
    getOrderMaintenanceList() {
      this.maintenanceStatuss = 'loading'
      uni.showLoading({
        title: '加载中',
        mask: true
      })

      this.$api.maintenanceOrder.OrderMaintenanceList(this.maintenance).then(res => {
        uni.hideLoading()
        this.maintenanceList = this.maintenanceList.concat(res.data.list)
        this.maintenanceTotal = res.data.total
        this.pageLoading = true
        if (res.data.list.length === 0) {
          this.showWb = true
        } else {
          this.showWb = false
        }

        if (Math.ceil(this.maintenanceTotal / this.maintenance.pageSize) > this.maintenance.pageIndex) {
          this.maintenanceStatuss = 'loadmore'
        } else {
          this.maintenanceStatuss = 'nomore'
        }
      }).catch(err => {
        uni.hideLoading()
      })
    },
    maintenMore() {
      if (Math.ceil(this.maintenanceTotal / this.maintenance.pageSize) > this.maintenance.pageIndex) {
        this.maintenance.pageIndex++
        this.getOrderMaintenanceList()
      } else {
        this.maintenanceStatuss = 'nomore'
      }

    },

    //取消预约
    async determine() {
      try {
        let data = {
          'bookId': this.id
        }
        const res = await this.$api.order.cancelCarBook(data)
        this.show = false
        this.$refs.uToast.show({
          title: '取消成功',
          type: 'success',
        })
        this.mentPageIndex = 1
        this.appointmentList = []
        this.getCarBookPageList()

      } catch (e) {

      }
    },
    //取消预约
    cancel(id) {
      console.log(id)
      this.id = id
      this.show = true
    },
    //预约切换
    changeYyTabs(index) {
      this.scrollTo()
      this.mentPageIndex = 1
      this.appointmentList = []
      this.current3 = index
      this.getCarBookPageList()
    },
    //预约数据
    async getCarBookPageList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      this.moreStatus('loading')
      try {
        let data = {
          'pageIndex': this.mentPageIndex,
          'pageSize': 10,
          'status': this.current3
        }
        const res = await this.$api.order.getCarBookPageList(data)
        this.appointmentList = [...this.appointmentList, ...res.data.list]
        this.pageLoading = true
        if (this.appointmentList.length === 0) {
          this.defaultYy = true
        } else {
          this.defaultYy = false
        }
        uni.hideLoading()
        uni.stopPullDownRefresh()

        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.mentPageIndex += 1 // 当前页加1

      } catch (e) {
        uni.hideLoading()
      }
    },

    // 寻车订单列表(全部)
    async getMyCarFindList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        this.moreStatus('loading')
        let data = {
          'pageIndex': this.findAllIndex,
          'pageSize': 10
        }
        const res = await this.$api.order.getMyCarFindList(data)

        this.findList = [...this.findList, ...res.data.list]
        this.pageLoading = true
        if (this.findList.length === 0) {
          this.findDefault = true
        } else {
          this.findDefault = false
        }
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.findAllIndex += 1 // 当前页加1
      } catch (e) {
        uni.hideLoading()
      }
    },
    // 寻车订单列表(待确认)
    async getMyCarFindNotQuoteList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        this.moreStatus('loading')
        let data = {
          'pageIndex': this.findAllIndex,
          'pageSize': 10
        }
        const res = await this.$api.order.getMyCarFindNotQuoteList(data)

        this.findList = [...this.findList, ...res.data.list]
        this.pageLoading = true
        if (this.findList.length === 0) {
          this.findDefault = true
        } else {
          this.findDefault = false
        }
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.findAllIndex += 1 // 当前页加1
      } catch (e) {
        uni.hideLoading()
      }
    },
    // 寻车订单列表(待报价)
    async getMyCarFindNotCertainList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        this.moreStatus('loading')
        let data = {
          'pageIndex': this.findAllIndex,
          'pageSize': 10
        }
        const res = await this.$api.order.getMyCarFindNotCertainList(data)

        this.findList = [...this.findList, ...res.data.list]
        this.pageLoading = true
        if (this.findList.length === 0) {
          this.findDefault = true
        } else {
          this.findDefault = false
        }
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.findAllIndex += 1 // 当前页加1
      } catch (e) {
        uni.hideLoading()
      }
    },
    // 寻车订单列表(已完成)
    async getMyCarFindFinishedList() {
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      try {
        this.moreStatus('loading')
        let data = {
          'pageIndex': this.findAllIndex,
          'pageSize': 10
        }
        const res = await this.$api.order.getMyCarFindFinishedList(data)

        this.findList = [...this.findList, ...res.data.list]
        this.pageLoading = true
        if (this.findList.length === 0) {
          this.findDefault = true
        } else {
          this.findDefault = false
        }
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (res.data.total <= 10) {
          this.statuss = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        if (res.data.list.length === 0) return false
        this.findAllIndex += 1 // 当前页加1
      } catch (e) {
        uni.hideLoading()
      }
    },
    changeFind(i) {
      this.scrollTo()
      this.findList = []
      this.findCurrent = i

      if (this.findCurrent === 0) {
        this.findAllIndex = 1
        this.getMyCarFindList()
      } else if (this.findCurrent === 1) {
        this.findAllIndex = 1
        this.getMyCarFindNotQuoteList()
      } else if (this.findCurrent === 2) {
        this.findAllIndex = 1
        this.getMyCarFindNotCertainList()
      } else if (this.findCurrent === 3) {
        this.findAllIndex = 1
        this.getMyCarFindFinishedList()
      }
    },

    // 维保订单切换
    maintenChange(e) {
      this.scrollTo()
      this.maintenCurNow = e
      this.maintenance.status = --e
      this.maintenance.pageIndex = 1
      this.maintenanceList = []
      this.getOrderMaintenanceList()
      // this.showWb=false
    }
  }
}
